package fr.orleans.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import entities.Referee;
import entities.Tournament;
import fr.orleans.tools.EJBServices;
import fr.orleans.tools.JSFServices;

@FacesConverter("refereeConverter")
public class RefereeConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2 != null && arg2 != ""){
			EJBServices services = (EJBServices) JSFServices.getManagedBean(EJBServices.class, "eJBServices");
			List<Referee> listReferre = services.getWebAction().getListRefereeByFlagActivated();
			
			for (Referee referee : listReferre) {
				if (referee.equals(arg2))
					return referee;		
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if(arg2 != null){
			if(arg2 instanceof Tournament){
				Referee tour = (Referee) arg2;
				return tour.toString();
			}
		}
		return null;
	}
}